package telemedicine
import grails.converters.* //for converter JSON
import java.util.regex.Pattern
import java.util.regex.Matcher
import com.webboard.*
import com.diseaseInformation.*

class MyformatToHtmlService {

    static transactional = true

    def findtag(def params) {
           
        def st=[]
        Pattern p;
        Matcher m;

        p = Pattern.compile("\\[[a-z]+_[^\\]]+\$");
        m = p.matcher(params['value']);
        
        while (m.find()) {
            
            String tag = m.group().split("_")[0].substring(1)
            String key = m.group().split("_")[1]
    
            if(tag.equals("d")){//findDisease
                st = Disease.executeQuery("select id,name from Disease as d where d.name like '"+key+"%'")
            }else if(tag.equals("p")){//findProblem
                st = Problem.executeQuery("select id,name from Problem as p where p.name like '"+key+"%'")
            }else if(tag.equals("m")){
                st = Management.executeQuery("select id,name from Management as m where m.name like '"+key+"%'")
            }else if(tag.equals("at")){
                st = Atlas.executeQuery("select id,name from Atlas as at where at.name like '"+key+"%'")
            }else if(tag.equals("g")){//find
                st = Disease_Group.executeQuery("select id,name from Disease_Group as g where g.name like '"+key+"%'")
            }else if(tag.equals("lab")){
                st = Laboratory.executeQuery("select id,name from Laboratory as l where l.name like '"+key+"%'")
            }else if(tag.equals("c")){
                st = CaseScenario.executeQuery("select id,name from CaseScenario as c where c.name like '"+key+"%'")
            }else if(tag.equals("ter")){
                st = Terminology.executeQuery("select id,name from Terminology as t where t.name like '"+key+"%'")
            }        
        }
        //System.out.println("st="+st)
        return st
    }
    
    
    def toHtmlTagWebboard(String s) {
        StringBuffer sb = new StringBuffer();
        s = s.replaceAll(" ","&nbsp;") // change spacebar
        s = s.replaceAll("	","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") // change tab
        //System.out.println(s);
        //Pattern pattern = Pattern.compile("\\[w_[a-z0-9]+]");
        Pattern pattern = Pattern.compile("\\[[a-z]+_[^\\]]+\\]")
        Matcher matcher = pattern.matcher(s)
        //System.out.println("Before s in tag webboard service : "+s);
        while (matcher.find()) {
                
            String tag = matcher.group().split("_")[0].substring(1)
            String key = matcher.group().split("_")[1]
            key = key.substring(0,key.length()-1)
            //System.out.println(matcher.group()+","+tag+","+key)
            
            if(tag.equals("d")){//findDisease
                if(Disease.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/disease/show/"+Disease.findByName(key).id+"\" onmouseover=\"over('disease',"+Disease.findByName(key).id+")\">"+key+"</a>")
            }else if(tag.equals("p")){//findProblem 
                if(Problem.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/problem/show/"+Problem.findByName(key).id+"\" onmouseover=\"over('problem',"+Problem.findByName(key).id+")\">"+key+"</a>")
            }else if(tag.equals("g")){
                if(Disease_Group.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/disease_Group/show/"+Disease_Group.findByName(key).id+"\" onmouseover=\"over('disease_Group',"+Disease_Group.findByName(key).id+")\">"+key+"</a>")
            }else if(tag.equals("lab")){
                if(Laboratory.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/laboratory/show/"+Laboratory.findByName(key).id+"\" onmouseover=\"over('laboratory',"+Laboratory.findByName(key).id+")\">"+key+"</a>")              
            }else if(tag.equals("c")){
                if(CaseScenario.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/caseScenario/show/"+CaseScenario.findByName(key).id+"\" onmouseover=\"over('caseScenario',"+CaseScenario.findByName(key).id+")\">"+key+"</a>")
            }else if(tag.equals("ter")){
                if(Terminology.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/terminology/show/"+Terminology.findByName(key).id+"\" onmouseover=\"over('terminology',"+Terminology.findByName(key).id+")\">"+key+"</a>")
            }else if(tag.equals("m")){
                if(Management.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/management/show/"+Management.findByName(key).id+"\" onmouseover=\"over('management',"+Management.findByName(key).id+")\">"+key+"</a>")
            }else if(tag.equals("at")){
                if(Atlas.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/atlas/show/"+Atlas.findByName(key).id+"\">"+"<img src=\"/telemedicine/atlas/image/"+Atlas.findByName(key).id+"\" style=\"max-width: 600px\" onmouseover=\"over('atlas',"+Atlas.findByName(key).id+")\">"+"</img></a>")
            }
          
        }
        matcher.appendTail(sb)
        String sout = sb.toString()
        
        //sout = sout.replaceAll("  ","&nbsp;&nbsp;")
        sout = sout.replaceAll("\\*([^\n]*)", "<ul>\n<li>\$1</li>\n</ul>" )
        sout = sout.replaceAll("\n</ul>[\n]*<ul>","")
        
        sout = sout.replaceAll("#([^\n]*)", "<ol>\n<li>\$1</li>\n</ol>" )
        sout = sout.replaceAll("\n</ol>[\n]*<ol>","")
        
        sout = sout.trim().replaceAll("\n","<br>")
        //sout=sout.trim().replaceAll("<b>","<br>")
        
        
        sout=sout.replaceAll("\\[","<")
        sout=sout.replaceAll("\\]",">")
        //System.out.println("After s in tag webboard service : "+sb);
        return sout
    }
    
    def toHtmlTagMobile(String s) {
        StringBuffer sb = new StringBuffer();
        s = s.replaceAll(" ","&nbsp;") // change spacebar
        s = s.replaceAll("	","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") // change tab

        Pattern pattern = Pattern.compile("\\[[a-z]+_[^\\]]+\\]")
        Matcher matcher = pattern.matcher(s)

        while (matcher.find()) {
                
            String tag = matcher.group().split("_")[0].substring(1)
            String key = matcher.group().split("_")[1]
            key = key.substring(0,key.length()-1)
            
            if(tag.equals("d")){//findDisease
                if(Disease.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/disease/mshow/"+Disease.findByName(key).id+"\">"+key+"</a>")
            }else if(tag.equals("p")){//findProblem 
                if(Problem.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/problem/mshow/"+Problem.findByName(key).id+"\">"+key+"</a>")
            }else if(tag.equals("g")){
                if(Disease_Group.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/disease_Group/mshow/"+Disease_Group.findByName(key).id+"\">"+key+"</a>")
            }else if(tag.equals("lab")){
                if(Laboratory.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/laboratory/mshow/"+Laboratory.findByName(key).id+"\">"+key+"</a>")              
            }else if(tag.equals("c")){
                if(CaseScenario.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/caseScenario/mshow/"+CaseScenario.findByName(key).id+"\">"+key+"</a>")
            }else if(tag.equals("ter")){
                if(Terminology.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/terminology/mshow/"+Terminology.findByName(key).id+"\">"+key+"</a>")
            }else if(tag.equals("m")){
                if(Management.findByName(key)!=null)
                matcher.appendReplacement(sb,"<a href=\"/telemedicine/management/mshow/"+Management.findByName(key).id+"\">"+key+"</a>")
            }else if(tag.equals("at")){
                if(Atlas.findByName(key)!=null)
                matcher.appendReplacement(sb,"<img src=\"/telemedicine/atlas/image/"+Atlas.findByName(key).id+"\">"+key+"</img>")
            }
          
        }
        matcher.appendTail(sb)
        String sout = sb.toString()
        
        //sout = sout.replaceAll("  ","&nbsp;&nbsp;")
        sout = sout.replaceAll("\\*([^\n]*)", "<ul>\n<li>\$1</li>\n</ul>" )
        sout = sout.replaceAll("\n</ul>[\n]*<ul>","")
        
        sout = sout.replaceAll("#([^\n]*)", "<ol>\n<li>\$1</li>\n</ol>" )
        sout = sout.replaceAll("\n</ol>[\n]*<ol>","")
        
        sout = sout.trim().replaceAll("\n","<br>")
        //sout=sout.trim().replaceAll("<b>","<br>")
        
        
        sout=sout.replaceAll("\\[","<")
        sout=sout.replaceAll("\\]",">")
        //System.out.println("After s in tag webboard service : "+sb);
        return sout
    }
    
    def autotag(def params){
        String data = params.myfield
        if(params['management']=='true')
        Management.list().each(){
            data = data.replaceAll("([^_]|^)"+it.name+"([^\\]]|\$)","\$1[m_"+it.name+"]\$2")
        }
        if(params['disease']=='true')
        Disease.list().each(){
            data = data.replaceAll("([^_]|^)"+it.name+"([^\\]]|\$)","\$1[d_"+it.name+"]\$2")
        }
        if(params['problem']=='true')
        Problem.list().each(){
            data = data.replaceAll("([^_]|^)"+it.name+"([^\\]]|\$)","\$1[p_"+it.name+"]\$2")
        }
        if(params['terminology']=='true')
        Terminology.list().each(){
            data = data.replaceAll("([^_]|^)"+it.name+"([^\\]]|\$)","\$1[ter_"+it.name+"]\$2")
        }
        if(params['laboratory']=='true')
        Laboratory.list().each(){
            data = data.replaceAll("([^_]|^)"+it.name+"([^\\]]|\$)","\$1[lab_"+it.name+"]\$2")
        }
        if(params['caseScenario']=='true')
        CaseScenario.list().each(){
            data = data.replaceAll("([^_]|^)"+it.name+"([^\\]]|\$)","\$1[c_"+it.name+"]\$2")
        }

        return data
    }

    def managementviewverinc(def id){
        def tmp = Management.get(id);
        tmp.viewer += 1;
        tmp.save();
    }
    def diseaseviewverinc(def id){
        def tmp = Disease.get(id);
        tmp.viewer += 1;
        tmp.save();
    }
    def atlasviewverinc(def id){
        def tmp = Atlas.get(id);
        tmp.viewer += 1;
        tmp.save();
    }
    def caseScenarioviewverinc(def id){
        def tmp = CaseScenario.get(id);
        tmp.viewer += 1;
        tmp.save();
    }
    def laboratoryviewverinc(def id){
        def tmp = Laboratory.get(id);
        tmp.viewer += 1;
        tmp.save();
    }
    def problemviewverinc(def id){
        def tmp = Problem.get(id);
        tmp.viewer += 1;
        tmp.save();
    }
    def terminologyviewverinc(def id){
        def tmp = Terminology.get(id);
        tmp.viewer += 1;
        tmp.save();
    }
    
}
